using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Cinemachine;
public class Plants : MonoBehaviour
{
    public Transform[] plants;
    public static bool IsPlants=true;
    private CinemachineVirtualCamera PlantsCamra;
    private CinemachineVirtualCamera MainCamra;
    public CinemachineVirtualCamera SunCamera;

    // Start is called before the first frame update
    void Start()
    {
        PlantsCamra = GameObject.Find("PlantsCamera").GetComponent<CinemachineVirtualCamera>();
        MainCamra = GameObject.Find("Main").GetComponent<CinemachineVirtualCamera>();
        SunCamera = GameObject.Find("SunCamera").GetComponent<CinemachineVirtualCamera>();
        plants = new Transform[this.transform.childCount];
        SelectChild();
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
       if(IsPlants == true)
        {
            if (Input.GetMouseButtonDown(0))
            {
                {
                    if (Physics.Raycast(ray, out hit))
                    {
                        CheckName(hit.collider.gameObject.name, hit.collider);
                        print(hit.collider.gameObject.name);
                    }
                    else
                    {
                        SelectChild();
                        PlantsCamra.gameObject.SetActive(false);
                        SunCamera.gameObject.SetActive(false);
                        MainCamra.gameObject.SetActive(true);
                    }

                }

            }
        }  
    }
    void CheckName(string name,Collider hitCollider)
    {       
        
        for (int i = 0; i < plants.Length; i++)
        {
            plants[i].gameObject.SetActive(false);
            if ( plants[i].name.ToLower() != hitCollider.transform.name)
            {
                plants[i].gameObject.SetActive(false);
            }
            else
            {
                MainCamra.gameObject.SetActive(false);
                PlantsCamra.gameObject.SetActive(true);
                SunCamera.gameObject.SetActive(false);
                plants[i].gameObject.SetActive(true);
                PlantsCamra.Follow = hitCollider.transform;
                PlantsCamra.LookAt = hitCollider.transform;
               if(hitCollider.transform.name=="sun")
                {
                 
                    PlantsCamra.gameObject.SetActive(false);
                   
                    SunCamera.gameObject.SetActive(true);
                    SunCamera.Follow = hitCollider.transform;
                    SunCamera.LookAt = hitCollider.transform;
                }

            }
        }
    }
    void SelectChild()
    {
       
        for (int i = 0; i < plants.Length; i++)
        {
            plants[i] = this.transform.GetChild(i);
            plants[i].gameObject.SetActive(false);
        }
    }
}
